package com.mdl.mall.controller;

import com.google.zxing.WriterException;
import com.mdl.mall.util.QRCodeUtil;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.IOException;

/**
 * @Desc
 * @Author madengling
 * @Time 2020/10/15 15:32
 */
@ApiIgnore
@RestController
@RequestMapping("/qr")
public class QRcodeController {

    @RequestMapping(value = "/hello",method = RequestMethod.GET)
    public void login(HttpServletResponse response)throws IOException{
//        String text = "http://recept.zjzwfw.gov.cn/oss//form/4ec73f42-9218-372c-b245-01eefb11d634.jpg";
        String text = "http://www.baidu.com";
//        String text = "哥，我喜欢你123132132";
        String localPng = "G://0921.jpg";
        BufferedImage image = null;
        try {
            image = QRCodeUtil.createImage(text,localPng,true);
        } catch (WriterException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        //以JPEG格式向客户端发送
        ServletOutputStream os = response.getOutputStream();
        ImageIO.write(image, "PNG",os);
        os.flush();
        os.close();
    }
}
